package com.shhm.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.benmanes.caffeine.cache.Cache;
import com.shhm.admin.service.RoleOperatePermsService;
import com.shhm.admin.util.RequestUtil;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.dto.request.admin.RolePermsReq;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.response.admin.SysRoleOperatePermsVO;
import com.shhm.common.entity.admin.SysRoleOperatePerms;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.utils.LocalCacheUtil;
import com.shhm.common.utils.RedisUtil;
import com.shhm.common.utils.ResponseUtil;
import com.shhm.common.utils.SysPermsUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/8/15 下午2:33
 */
@Service
public class RoleOperatePermsServiceImpl implements RoleOperatePermsService {
    @Resource
    private SysRoleOperatePermsMapper sysRoleOperatePermsMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource(name = "PermissionsCache")
    private Cache<String, List<SysRoleOperatePerms>> localPermissionsCache;

    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, SysRoleOperatePermsVO entity) {
        PageDTO<SysRoleOperatePermsVO> defPage = new PageDTO<>(pageNum, pageSize);
        return AjaxResultPlus.success(sysRoleOperatePermsMapper.pageSearchList(defPage,entity),false);
    }

    @Override
    public AjaxResultPlus pageList(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageDTO<SysRoleOperatePermsVO> defPage = new PageDTO<>(pageNum, pageSize);
        PageDTO<SysRoleOperatePermsVO> list = sysRoleOperatePermsMapper.pageList(defPage);
        return AjaxResultPlus.custom(200, false)
                .put("data",list);
    }

    @Deprecated
    @Override
    public AjaxResultPlus add(SysRoleOperatePerms entity) {
        return ResponseUtil.sqlExeIsSuccess(sysRoleOperatePermsMapper.add(entity));
    }

    @Override
    public AjaxResultPlus update(SysRoleOperatePerms entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(sysRoleOperatePermsMapper.updateById(entity));
        SysPermsUtil.flushOperatePermsCache(localPermissionsCache,redisTemplate,sysRoleOperatePermsMapper);
        return res;
    }

    @Override
    public AjaxResultPlus add(RolePermsReq entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(sysRoleOperatePermsMapper.addData(entity));
        SysPermsUtil.flushOperatePermsCache(localPermissionsCache,redisTemplate,sysRoleOperatePermsMapper);
        return res;
    }
    @Deprecated
    @Override
    public AjaxResultPlus update(RolePermsReq entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(sysRoleOperatePermsMapper.updateData(entity));
        SysPermsUtil.flushOperatePermsCache(localPermissionsCache,redisTemplate,sysRoleOperatePermsMapper);
        return res;
    }

    @Override
    public AjaxResultPlus delete(RequestList ids) {
        List<Object> list = ids.getList();
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(sysRoleOperatePermsMapper.deleteByIds(list));
        SysPermsUtil.flushOperatePermsCache(localPermissionsCache,redisTemplate,sysRoleOperatePermsMapper);
        return res;
    }

    private void flushPermsRedis(){
        List<SysRoleOperatePerms> list = sysRoleOperatePermsMapper.selectList(null);
        LocalCacheUtil.flushOpePermsLocalCache(localPermissionsCache,list);
        RedisUtil.flushSysRoleOperatePerms(list,redisTemplate);
    }
}
